﻿
@{
    Layout = "~/Views/Shared/_wxDetail.cshtml";
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
    }
    <link href="/SiteUI/Trade/DeliveryBill.css" rel="stylesheet" />
}
<title>发货单</title>

<div class="view screen">
    <div class="row">
        <div class="clo-sm-12 detail">

        </div>
    </div>
    <div class="row foot">
        <div class="col-sm-12 button" obj-auth="true" obj-role="consignee" obj-status="Active" execute="disable" id="btnReceipt">
            收货确认
        </div>
        <div class="col-sm-12 button disabled" obj-status="Completed">
            已收货
        </div>
    </div>
</div>

<script type="text/html" id="tplDeliveryBill">
    <div class="row" id="orderHead">
        <div class="col-sm-12">
            <h4>发货单号：<strong id="oh-dlvid">{{d._dlvid}}</strong></h4>
        </div>
        <div class="col-sm-12">
            <h4>发货时间：<strong id="oh-dlvdate">{{d.thisDlvDate}}</strong></h4>
        </div>
    </div>

    <!--订单详情-->
    <div class="row">
        <div class="InfoSectionBody col-sm-12" id="tradeInfo">
            <div class="row ti-body">
                <div class="col-sm-12 InfoSection">
                    <div class="DataField">
                        <span class="df-title">采&nbsp;&nbsp;购&nbsp;&nbsp;商：</span>
                        {{#if(d.client_realname){ }}
                        <span class="df-value"><a class="Company certificated" href="/Company/Portal?oid={{d.client_idstr}}">{{d.client_realname}}</a></span>
                        {{#}else{ }}
                        <span class="df-value"><a class="Company no-certificated" href="#">{{d.src_client}}</a></span>
                        {{#} }}
                    </div>
                    <div class="DataField">
                        <span class="df-title">联&nbsp;&nbsp;系&nbsp;&nbsp;人：</span>
                        {{#if(d.buyer){ }}
                        <span class="df-value"><a href="#">{{d.buyer.realname}}</a></span>
                        {{#}else{ }}
                        <span class="df-value"><a href="#">{{d.src_buyer}}</a></span>
                        {{#} }}
                    </div>
                    <div class="DataField">
                        <span class="df-title">联系方式：</span>
                        <span class="df-value">{{d.buyer_phone}}</span>
                    </div>
                </div>
                <div class="col-sm-12 InfoSection">
                    <div class="DataField">
                        <span class="df-title">收&nbsp;&nbsp;货&nbsp;&nbsp;人：</span>
                        <span class="df-value">
                            <span>{{d.consignee_displayname}}</span>
                            <span>{{d.consignee_phone}}</span>
                        </span>
                    </div>
                    <div class="DataField">
                        <span class="df-title">施工单位：</span>
                        <span class="df-value">
                            <span>{{d.constructor}}</span>
                        </span>
                    </div>
                    <div class="DataField">
                        <span class="df-title">交付方式：</span>
                        <span class="df-value">{{d.deliverymode}}</span>
                    </div>
                    <div class="DataField">
                        <span class="df-title">送货地址：</span>
                        <span class="df-value">{{d.src_dlvaddress}}</span>
                    </div>
                    <div class="DataField">
                        <span class="df-title">送货时间：</span>
                        {{#if(d._urgent){ }}
                        <span class="df-value urgent" title="交货日期已临近，请注意跟进">{{d._dlvDate}}<i class="icon-erroroutline"></i></span>
                        {{#}else{ }}
                        <span class="df-value">{{d._dlvDate}}</span>
                        {{#} }}
                    </div>
                    <div class="DataField">
                        <span class="df-title">质保条件</span>
                        <span class="df-value">{{d.qualityguarantee}}</span>
                    </div>
                </div>
            </div>

            <div class="col-sm-12 InfoSection">
                {{#if(d.src_orderserial){ }}
                <div class="DataField">
                    <span class="df-title">客户订单：</span>
                    <span class="df-value">{{d.src_orderserial}}{{d.src_orderversion}}</span>
                </div>
                {{#} }}
                <div class="DataField">
                    <span class="df-title">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：</span>
                    <span class="df-value">{{d.memo}}</span>
                </div>
            </div>

        </div>
    </div>

    <!--发货单明细-->
    <div class="InfoSection row expand" id="orderItems">
        <div class="InfoSectionTitle col-sm-12">
            发货明细
            <i class="soicon "></i>
        </div>
        <div class="InfoSectionBody col-sm-12">
            <ol></ol>
            <li class="wx-order-item-tpl">
                <h5 class="oi-title"></h5>
                <span class="oi-model"></span>
                <br />
                <span class="oi-productSpec"></span>
                <div class="oi-mx-dlvmxinfo">
                    <span class="oi-mx-count">订货量：</span>
                    <span class="oi-mx-thisdelivery">发货量：</span>
                    @*<span class="oi-mx-totaldelivery">累计已发：</span>*@
                </div>
                <div class="oi-mx-receipt">
                    <label>收货数量：</label>
                    <div>
                        <input obj-auth="true" obj-role="consignee" execute="disable" type="number" />
                    </div>

                </div>
                <div class="oi-mx-remark">
                    <label>备注：</label>
                    <div>
                        <input obj-auth="true" obj-role="consignee" execute="disable" />
                    </div>
                </div>
                <div class="floatClear">
                </div>
            </li>
        </div>
    </div>
</script>


<script src="/LiberUI/Scripts/laytpl.js"></script>
<script src="/SiteUI/res_sobiz.js"></script>
<script>
    var dlvBill = {};
    var order = {};
    var ViewDB = {
        tplDlvInfo: null,
        btnReceipt: null,
        GetDeliveryEnd: function (rtn) {
            if (rtn.IsPositive) {
                dlvBill = rtn.Data;
                order = dlvBill.order;
                order.buyer_phone = order.buyer_phone || "-";
                order.buyer_email = order.buyer_email || "-";
                order.saler_phone = order.saler_phone || "-";
                order.saler_email = order.saler_email || "-";
                order.consignee_displayname = order.src_consignee || order.consignee_realname;
                order.consignee_phone = order.consignee_phone || order.src_consigneephone;
                order._dlvDate = new Date(order.src_deliverydate || order.deliverydate).format("YYYY/MM/DD");
                order.thisDlvDate = new Date(dlvBill.updatetime).format("YYYY/MM/DD");
                order._dlvid = dlvBill.idstr;
                order._urgent = daysBetween(new Date(order.src_deliverydate || order.deliverydate).toString(), new Date().toString()) < 5;
                if (order.src_subconstuctor) {
                    order.src_constructor += "(" + order.src_subconstuctor + ")";
                }
                order.constructor = order.src_constructor || "-"
                
                var ti = ViewDB.tplDlvInfo.render(order);
                $(".detail").append(ti).fadeIn();

                //#region 处理订单状态信息
                if (dlvBill.status == 'Active') {
                    ViewDB.btnReceipt.vl("收货确认");
                }
                else {
                    ViewDB.btnReceipt.vl("已收货").disable();
                }

                $("#oh-dlvid").vl(dlvBill.idstr);
                $("#oh-status").vl("<i></i>" + res_sobiz.var.Sale.Delivery[dlvBill.status]);
                //$("#lnkDeliveryPrnt").attr("href", "/Sale/DeliveryBillPrnt?oid=" + dlvBill.idstr);
                //#endregion

                ViewDB.renderOrderMX();
                View.CheckAuthor(order);
            }
        },
        renderOrderMX: function () {
            //#region 处理发货单明细
            if (dlvBill.deliverymx) {
                var tpl = $(".wx-order-item-tpl").eq(0);
                    var tplMX = $(".wx-order-item-tpl").eq(0).remove();
                    var gridBody = $("#orderItems").find("ol").eq(0);
                    if (gridBody.children("li").length <= 1) {
                        dlvBill.deliverymx.forEach(function (i) {
                            var row = tplMX.clone();
                            row.children(".oi-title").vl(i.brand + '【' + i.product + '】');
                            row.children(".oi-model").vl("型号：" + i.model);
                            row.children(".oi-productSpec").vl("规格：" + i.src_specification || "-");
                            row.find(".oi-mx-count").vl("订货量：" + i.count + '[' + i.unit + ']');
                            var dlvmx = dlvBill.deliverymx.findObj("idstr", i.idstr);
                            row.find(".oi-mx-thisdelivery").vl("&nbsp;|&nbsp;发货量：" + dlvmx.todelivery);
                            //row.find(".oi-mx-totaldelivery").vl("&nbsp;|&nbsp;累计已发：" + i.totaldeliveried);
                            row.children(".oi-mx-receipt").find("input").vl(dlvmx.todelivery);
                            row.appendTo(gridBody).show();

                            if (dlvBill.status !== 'Active') {
                                row.children(".oi-mx-receipt").find("input").disable();
                                row.children(".oi-mx-remark").find("input").vl(dlvmx.dlv_remark||"-").disable();
                            }

                            dlvmx.$row = row;
                            dlvBill.deliverymx.findObj("idstr", i.idstr,dlvmx);
                        });
                    }
            }
            //#endregion
        },
        onInsecClick: function (ev) {
            $(this).parent(".InfoSection").toggleClass("expand");
        },
        Receipt: function (ev) {
            var rcptMX = [];
            dlvBill.deliverymx.forEach(function (i) {
                var receipt = i.$row.children(".oi-mx-receipt").find("input").vl();
                var rmrk = i.$row.children(".oi-mx-remark").find("input").vl();
                var rcptCount = parseFloat(receipt);
                rcptMX.push(
                    {
                        "ordermxidstr":i.idstr,
                        "dlvmxidstr": i.dlvmxidstr,
                        "receipt": rcptCount >= 0 ? rcptCount : 0,
                        "remark": rmrk|| ""
                    }
                );
            })
            //发送
            $.post("/Purchase/ReceiptDelivery", { "rcptMX": GetXML({ "item": rcptMX, "dlvid": dlvBill.idstr,"orderid":order.idstr }) }, function () {
                window.history.go(0);
            });
        }
    }
    $(function () {
        var oid = getUrlParam("oid");

        $("body").on("UserReady", function () {
            $.getJSON("/Trade/GetDelivery", { 'oid': oid }, ViewDB.GetDeliveryEnd);
        });

        $(".InfoSectionTitle").on("click", ViewDB.onInsecClick);

        ViewDB.tplDlvInfo = laytpl($("#tplDeliveryBill")[0].innerHTML);

        ViewDB.btnReceipt = new LiberUI.Button("#btnReceipt")
                                       .on("LCclick", ViewDB.Receipt);
    })
</script>

